﻿using PhotoGallery.Data.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Security;
using PhotoGallery.Areas.Auth.Models;
using PhotoGallery.Data.Entity;
using PhotoGallery.Models.Utils;
using Microsoft.Web.WebPages.OAuth;
using PhotoGallery.Infrastructure;

namespace PhotoGallery.Models
{

    public class ChangePasswordModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "New password")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm new password")]
        //[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

    public class LogOnModel
    {
        PhotoGallery.Data.Repository.UserRepository repository;

        public LogOnModel()
        {
            repository = new UserRepository();
        }

        [Required]
        [LocalizedDisplayName("username", NameResourceType = typeof(Names))]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [LocalizedDisplayName("password", NameResourceType = typeof(Names))]
        public string Password { get; set; }

        [LocalizedDisplayName("rememberMe", NameResourceType = typeof(Names))]
        public bool RememberMe { get; set; }

        public bool ValidateUser(string username, string password, out string localUserName)
        {
            User user = repository.Get(username, password);
            if (user != null)
            {
                localUserName = user.Username;
                return true;
            }
            else
            {
                localUserName = "";
                return false;
            }
        }
    }

    public class RegisterModel
    {
        UserRepository repository;

        public RegisterModel()
        {
            repository = new UserRepository();
        }

        [Required]
        [LocalizedDisplayName("username", NameResourceType = typeof(Names))]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [LocalizedDisplayName("email", NameResourceType = typeof(Names))]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
        [DataType(DataType.Password)]
        [LocalizedDisplayName("password", NameResourceType = typeof(Names))]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        //[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        [LocalizedDisplayName("confirmPassword", NameResourceType = typeof(Names))]
        public string ConfirmPassword { get; set; }

        public bool ValidateUser(string username, string password)
        {
            return repository.Get(username, password) != null;
        }

        public bool CreateUser(string username, string password, string email, out string errorMessage)
        {
            errorMessage = "";
            if (repository.GetByUsername(username) != null)
            {
                errorMessage = "The user name already exists in the database for the application.";
                return false;
            }
            if (repository.GetByUsername(email) != null)
            {
                errorMessage = "The e-mail address already exists in the database for the application.";
                return false;
            }
            User user = new User(username, email, password);

            return repository.Create(user);
        }

    }

    public class LoginModel
    {
        UserRepository repository;

        public LoginModel()
        {
            repository = new UserRepository();
        }

        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }

        public bool ValidateUser(string username, string password)
        {
            return repository.Get(username, password) != null;
        }
    }

    public class RegisterExternalLoginModel
    {
        UserRepository repository;

        public RegisterExternalLoginModel()
        {
            repository = new UserRepository();
        }

        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        public string ExternalLoginData { get; set; }

        public bool CreateUser(string username, string password, string email, out string errorMessage)
        {
            errorMessage = "";
            if (repository.GetByUsername(username) != null)
            {
                errorMessage = "The user name already exists in the database for the application.";
                return false;
            }
            if (repository.GetByUsername(email) != null)
            {
                errorMessage = "The e-mail address already exists in the database for the application.";
                return false;
            }
            User user = new User(username, email, password);

            return repository.Create(user);
        }
    }

    public class LocalPasswordModel
    {
        UserRepository repository;

        public LocalPasswordModel()
        {
            repository = new UserRepository();
        }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "New password")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm new password")]
        //[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }

        public bool HasLocalAccount(string userName)
        {
            return repository.GetByUsername(userName) != null;
        }

        public bool CreateAccount(string username, string password)
        {
            if (repository.GetByUsername(username) != null)
            {
                return false;
            }
            User user = new User(username, password);

            return repository.Create(user);
        }

        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            User user = repository.GetByUsername(userName);
            if (user != null)
            {
                user.Password = newPassword;
                repository.Edit(user);
                return true;
            }
            else
            {
                return false;
            }
        }

        public ExternalLogin GetAccount(string providerUserId)
        {
            ExternalLoginRepository repo = new ExternalLoginRepository();
            return repo.Get(providerUserId); 
        }
        

        public ICollection<OAuthAccount> GetAccountsFromUserName(string userName)
        {
            List<OAuthAccount> accounts = new List<OAuthAccount>();
            ExternalLoginRepository repo = new ExternalLoginRepository();
            IEnumerable<ExternalLogin> logins = repo.GetByUsername(userName);
            foreach(var item in logins)
            {
                accounts.Add(new OAuthAccount(item.Provider,item.ProviderUserId));
            }
            return accounts;

        }

        public void CreateOrUpdateAccount(string provider,string providerUserId, string providerDisplayName, string localUserName)
        {
            ExternalLoginRepository repo = new ExternalLoginRepository();
            ExternalLogin login = repo.Get(provider, localUserName);
            if (login == null)
            {
                repo.Create(new ExternalLogin(provider,providerUserId, providerDisplayName, localUserName));
            }
            else
            {
                // TODO Update
            }
        }
    }
}
